using System.Collections;
using Game.Framework.FState;
using UnityEngine;
using YooAsset;

namespace Game.Framework
{
    public class StateYooAssetUpdatePackageManifest : StateYooAssetBase
    {
        public AssetData _data;
        public override void Enter(StateData param)
        {
            base.Enter(param);
            _data = param as AssetData;
            YooAssetManager.Instance.StartCoroutine(UpdateManifest());
        }
        
        public override void FixedUpdate(float deltaTime)
        {
            base.FixedUpdate(deltaTime);
        }
        
        public override void Update()
        {
            base.Update();
        }
        
        public override void Exit()
        {
            base.Exit();
        }

        private IEnumerator UpdateManifest()
        {
            yield return new WaitForSecondsRealtime(0.5f);
            var package = YooAssets.GetPackage(AssetConfig.Instance.PackageName);
            var operation = package.UpdatePackageManifestAsync(_data.PackageVersion);
            yield return operation;
            if (operation.Status != EOperationStatus.Succeed)
            {
                EventBus.Send(new EventUpdatePackageManifestFailed(operation.Error));
            }
            else
            {
                if (AssetConfig.Instance.RunMode == EPlayMode.EditorSimulateMode ||
                    AssetConfig.Instance.RunMode == EPlayMode.OfflinePlayMode)
                {
                    EventBus.Send<EventNoAssetNeedDownLoad>();
                }
                else
                {
                    ChangeState<StateYooAssetDownloader>(null);
                }
                EventBus.Send<EventUpdatePackageManifestSucceed>();
            }
        }
    }
}


